#include <map>
#include <iostream>
using namespace std;

//解法1：STL map
void demo1(){
    map<int, int> m;//<id,等级>
    int n;
    while(~scanf("%d", &n) && n){
        m.clear();
        m[1000000000] = 1;//方丈1，等级1000000000
        while(n--){
            int id, g;
            scanf("%d %d", &id, &g); 
            m[g] = id;  //新和尚
            int ans;
            //找到排好序的位置
            map<int, int>::iterator it = m.find(g);
            if(it == m.begin()){
                ans = (++it)->second;
            }else{
                map<int, int>::iterator it2 = it;
                //等级接近的前后两个老和尚
                it2--;
                it++;
                if(g - it2->first <= it->first - g){
                    ans = it2->second;
                }else{
                    ans = it->second;
                }
            }
            printf("%d %d\n", id, ans);
        }
    }
}

int main(){
    demo1();
    return 0;
}